package com.linewell.controller;

import com.linewell.pojo.Msg;
import com.linewell.pojo.UserInfo;
import com.linewell.service.IUserService;
import com.linewell.util.Base64;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.validation.FieldError;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Title: ssm2
 * @description: 用户管理控制器
 * @author: hyuhuang@linewell.com
 * @since:2018/2/12
 */
@Controller
public class UserInfoController {
	
	@Autowired
	IUserService userService;

	/**
	 * @descripition  保存用户名
	 * @author hyuhuang@linewell.com
	 * @date 2018/2/27 10:55
	 * @param userInfo, result
	 * @return com.linewell.utils.Msg
	 */
	@RequestMapping(value="/UserInfo",method= RequestMethod.POST)
	@ResponseBody
	public Msg saveUserInfo(@Valid UserInfo userInfo, BindingResult result,HttpServletRequest request) {
		String code = request.getParameter("Vcode");
		String Vcode = String.valueOf(request.getSession().getAttribute("code"));
		if (result.hasErrors()) {
			List<FieldError> errors = result.getFieldErrors();
			Map<String, Object> map = new HashMap<String, Object>();
			for(FieldError fieldError : errors){
				//System.out.println("错误字段名" + fieldError.getField() + "错误信息" + fieldError.getDefaultMessage());
				map.put(fieldError.getField(), fieldError.getDefaultMessage());
			}
			return Msg.fail().add("FieldErrors", map);
		} else if (!Vcode.equals(code)){
			return Msg.fail1();
		} else {
			return userService.saveUserInfo(userInfo);
		}
	}
	/**
	 * @descripition 检验是否有重名的用户名
	 * @author hyuhuang@linewell.com
	 * @date 2018/2/27 10:55
	 * @param punsterName
	 * @return com.linewell.utils.Msg
	 */
	@RequestMapping("/checkuser")
	@ResponseBody
	public Msg checkuser(@RequestParam("punsterName")String punsterName){
		//验证正则表达式
		String regex = "(^[a-zA-Z0-9_-]{6,16}$)|([\u2E80-\u9FFF]{2,5})";
		if(!punsterName.matches(regex)){
			return Msg.fail().add("va_msg", "用户名6-16位或2-5个中文");
		}
		//判断是否有这个用户名
		UserInfo userInfo  = userService.checkPunsterName(punsterName);
		if(userInfo == null){
			return Msg.success();
		}else{
			//System.out.println(Msg.fail().getCode());
			return Msg.fail().add("va_msg", "用户名已存在");
		}
	}
	/**
	 * @descripition  更换用户名头像
	 * @author hyuhuang@linewell.com
	 * @date 2018/2/27 10:55
	 * @param image, userId, request
	 * @return com.linewell.utils.Msg
	 */
	@RequestMapping("/uploadPicture")
	@ResponseBody
	public Msg uploadPicture(String image, Long userId,HttpServletRequest request){
		//输出base64 数据,截取","之后的值进行转换
		String str = image.substring(image.indexOf(",")+1);
		//System.currentTimeMillis()意思是获取当前系统的时间戳给图片命名
		//实例化Base64转换类  调用里面的GenerateImage方法（把base64数据转为图片）
		//第一个参数base64转图片的必须的base64数据，第二个是转换后生成的图片存放路径
		String time = System.currentTimeMillis()+".gif";
		String imageName = request.getSession().getServletContext().getRealPath("/images/") + time;
		Base64.GenerateImage(str, imageName);
		boolean flag = userService.uploadUserPicture("images/" + time,userId);
		if (flag){
			return Msg.success().add("img","images/" + time);
		} else {
			return Msg.fail();
		}
	}

	@RequestMapping("/getHotUser")
	@ResponseBody
	public Msg getHotUser(@RequestParam("page") int page ){
		return userService.getHotUser(page);
	}

	@RequestMapping("/updataUserInfo")
	@ResponseBody
	public Msg updataUserInfo(@RequestParam("userId") String userId, @RequestParam("nickName") String nickName,
							  @RequestParam("sex") String sex,@RequestParam("birthday") String birthday,
							  @RequestParam("userEmail") String userEmail,@RequestParam("myself") String myself ){

		return userService.updataUserInfo(userId,nickName,sex,birthday,userEmail,myself);
	}

	@RequestMapping("/changePwd")
	@ResponseBody
	public Msg changePwd(@RequestParam("userId")Long userId,@RequestParam("oldPwd")String oldPwd,@RequestParam("newPwd")String newPwd){
		return userService.changePwd(userId,oldPwd,newPwd);
	}
	@RequestMapping("/updatePwd")
	public String updatePwd(){
		return "changePwd";
	}


}
